package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Picture;
import com.example.entity.Repair;
import com.example.mapper.PictureMapper;
import com.example.mapper.RepairMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账单表业务处理
 **/
@Service
public class WorkerRepairService {

    @Resource
    private RepairMapper repairMapper;
    @Resource
    private PictureMapper pictureMapper;

    /**
     * 新增
     */
    @Transactional
    public void add(Repair repair) {
        repair.setReportTime(DateUtil.now());
        // TODO 报修单号
        // 获取当前的日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间的格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        repair.setRepairNumber("RO"+now.format(formatter));
//        Account currentUser = TokenUtils.getCurrentUser();
//        if (RoleEnum.USER.name().equals(currentUser.getRole())) {  // 只有用户才记录用户ID
//            bill.setUserId(currentUser.getId());
//        }

        repairMapper.insert(repair);

        List<String> pictures = repair.getPictures();
        for(String temp : pictures){
            Picture picture = new Picture();
            picture.setRepairNumber(repair.getRepairNumber());
            picture.setCover(temp);
            picture.setType("USER");
            pictureMapper.insert(picture);
        }

    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        repairMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            repairMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Repair repair) {
        // TODO 添加已处理状态
        repair.setRepairStatus("已处理");
        repairMapper.updateById(repair);

        List<String> pictures = repair.getPictures();
        if(pictures!=null&& !pictures.isEmpty()) {
            for (String temp : pictures) {
                Picture picture = new Picture();
                picture.setRepairNumber(repair.getRepairNumber());
                picture.setCover(temp);
                picture.setType("WORKER");
                pictureMapper.insert(picture);
            }
        }
    }

    /**
     * 根据ID查询
     */
    public Repair selectById(Integer id) {
        return repairMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Repair> selectAll(Repair repair) {
        return repairMapper.selectAll(repair);
    }

    /**
     * 分页查询
     */
    public PageInfo<Repair> selectPage(Repair repair, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Repair> list = repairMapper.selectAll(repair);
        List<Repair> resList = new ArrayList<>();
        for (Repair repair1 : list) {
            Picture picture = new Picture();
            picture.setRepairNumber(repair1.getRepairNumber());
            picture.setType("USER");
            List<Picture> pictures = pictureMapper.selectAll(picture);
            List<String> reportImages = pictures.stream().map(Picture::getCover)
                    .collect(Collectors.toList());
            repair1.setReportImages(reportImages);
            picture.setType("WORKER");
            List<Picture> picturesW = pictureMapper.selectAll(picture);
            List<String> repairImages = picturesW.stream().map(Picture::getCover)
                    .collect(Collectors.toList());
            repair1.setRepairImages(repairImages);
            resList.add(repair1);
        }
        return PageInfo.of(resList);
    }

    public List<Repair> count(String type) {
        Repair repair = new Repair();
        repair.setRepairType(type);
        List<Repair> repairList = repairMapper.selectAll(repair);  // 根据当前登录的用户ID查询到所有的账单信息
        List<String> categoryList = repairMapper.selectCategoryByType(type);
        List<Repair> list = new ArrayList<>();
//        for (String category : categoryList) {
//            Repair r = new Repair();
//            r.setCategory(category);
//            // 统计出当前这个分类的所有的金额汇总
//            BigDecimal categorySum = billList.stream().filter(bi -> bi.getCategory().equals(category)).map(Bill::getMoney).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
//            // 返回当前分类的总金额
//            b.setMoney(categorySum);
//            // 得到账单的百分比
//            b.setPercent(categorySum.divide(sum, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).intValue());
//            list.add(b);
//        }
        return list;
    }

}